package org.jeecg.modules.demo.dtwlwjc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglEquipmentStatus;
import org.jeecg.modules.demo.dtwlwjc.mapper.ZwglEquipmentStatusMapper;
import org.jeecg.modules.demo.dtwlwjc.service.IZwglEquipmentStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 设备状态
 * @Author: jeecg-boot
 * @Date:   2023-04-17
 * @Version: V1.0
 */
@Service
public class ZwglEquipmentStatusServiceImpl extends ServiceImpl<ZwglEquipmentStatusMapper, ZwglEquipmentStatus> implements IZwglEquipmentStatusService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ZwglEquipmentStatusMapper zwglEquipmentStatusMapper;
    @Autowired
    private ZwglPestCountServiceImpl zwglPestCountService;

    @Override
    public String getzhsj() {
        String[] arr;
        arr = new String[12];
        arr[0] = "0511220007";
        arr[1] = "0512220003";
        arr[2] = "0511220001";
        arr[3] = "0511220008";
        arr[4] = "0511220006";
        arr[5] = "0514220002";
        arr[6] = "0512220005";
        arr[7] = "0512220006";
        arr[8] = "0514220001";
        arr[9] = "0514220004";
        arr[10] = "0511220002";
        arr[11] = "0511220003";
        for (int i = 0; i < arr.length; i++) {

            //得到long类型当前时间
            long l = System.currentTimeMillis();
            //new日期对象
            Date date = new Date(l);
            Date date1 = new Date(l);
            //转换提日期输出格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = dateFormat.format(date);
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time1 = dateFormat1.format(date1);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            //调用的api的接口地址
            String deviceAddr = arr[i];
            String startTime = dateFormat.format(calendar.getTime());
            String endTime = time;
            String apiPath = "http://farm.0531yun.cn/UserDevice/WormDeviceRealData?deviceAddr="+deviceAddr;
            BufferedReader in = null;
            StringBuffer result = null;
            String token = (String) redisUtil.get("dttoken");
            if (Objects.isNull(token)) {
                zwglPestCountService.getToken();
            }

            try {
                URL url = new URL(apiPath);
                //打开和url之间的连接

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);


                connection.setRequestMethod("GET");
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setRequestProperty("token", token);
                connection.setRequestProperty("Charset", "utf-8");
                connection.connect();
                result = new StringBuffer();
                //读取URL的响应
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
                String result2 = result.toString(); //返回json字符串

                //获取数据
                JSONObject jsonObject = JSON.parseObject(result2);
                if (jsonObject.getJSONObject("data")!=null){
                    JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                    ZwglEquipmentStatus zwglEquipmentStatus=new ZwglEquipmentStatus();
                    zwglEquipmentStatus.setDeviceName(jsonObject1.getString("deviceAddr"));
                    zwglEquipmentStatus.setDeviceStatus(jsonObject1.getString("deviceAddr"));
                    if (jsonObject1.getString("mode").equals("1")){
                        zwglEquipmentStatus.setRunningMode("自动");
                    }else{
                        zwglEquipmentStatus.setRunningMode("手动");
                    }
                    zwglEquipmentStatus.setInsecticideCompartment(jsonObject1.getString("insecticideTem"));
                    zwglEquipmentStatus.setDryingChamber(jsonObject1.getString("dryingTem"));
                    zwglEquipmentStatus.setElectricity(jsonObject1.getString("cell"));
                    if (jsonObject1.getString("rain").equals("0")){
                        zwglEquipmentStatus.setRainfallStatus("无雨");
                    }else{
                        zwglEquipmentStatus.setRainfallStatus("有雨");
                    }
                    zwglEquipmentStatus.setIllumination(jsonObject1.getString("illum"));
                    if (jsonObject1.getString("attractWorm").equals("0")){
                        zwglEquipmentStatus.setAttraction("关闭");
                    }else{
                        zwglEquipmentStatus.setAttraction("开启");
                    }
                    if (jsonObject1.getString("wormFlap").equals("0")){
                        zwglEquipmentStatus.setInsecticide("关闭");
                    }else{
                        zwglEquipmentStatus.setInsecticide("开启");
                    }
                    if (jsonObject1.getString("wormFlap").equals("0")){
                        zwglEquipmentStatus.setInsecticidalControl("关闭");
                    }else{
                        zwglEquipmentStatus.setInsecticidalControl("开启");
                    }
                    if (jsonObject1.getString("dryingFlap").equals("0")){
                        zwglEquipmentStatus.setDry("关闭");
                    }else{
                        zwglEquipmentStatus.setDry("开启");
                    }
                    if (jsonObject1.getString("shake").equals("0")){
                        zwglEquipmentStatus.setVibrationDevice("关闭");
                    }else{
                        zwglEquipmentStatus.setVibrationDevice("开启");
                    }
                    if (jsonObject1.getString("moveWorm").equals("0")){
                        zwglEquipmentStatus.setInsectMoving("关闭");
                    }else{
                        zwglEquipmentStatus.setInsectMoving("开启");
                    }
                    if (jsonObject1.getString("fillLight").equals("0")){
                        zwglEquipmentStatus.setInsectRain("关闭");
                    }else{
                        zwglEquipmentStatus.setInsectRain("开启");
                    }
                    if (jsonObject1.getString("rainFlap").equals("0")){
                        zwglEquipmentStatus.setFillinLight("关闭");
                    }else{
                        zwglEquipmentStatus.setFillinLight("开启");
                    }
                    zwglEquipmentStatus.setDeteTime(new Date());
                    zwglEquipmentStatusMapper.insert(zwglEquipmentStatus);
                }else{
                    System.out.println("无数据");
                }






            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
        return null;
    }


    @Override
    public Result getcqsbbh(){
        QueryWrapper<ZwglEquipmentStatus> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("device_name,device_status");
        List<ZwglEquipmentStatus> list=zwglEquipmentStatusMapper.selectList(queryWrapper);
        return Result.OK(list);
    }
    @Override
    public Result getcqss(String deviceName){
        QueryWrapper<ZwglEquipmentStatus> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("device_name",deviceName);
        queryWrapper.orderByDesc("dete_time");
        queryWrapper.last("limit 1");
        List<ZwglEquipmentStatus> list=zwglEquipmentStatusMapper.selectList(queryWrapper);
        return Result.OK(list);
    }
}
